package com.zjw.zy.componse.demo.fragment

import android.widget.ImageView
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.lifecycle.viewmodel.compose.viewModel
import com.blankj.utilcode.util.ToastUtils
import com.zjw.zy.componse.demo.R
import com.zjw.zy.componse.demo.base.BaseComposeFragment
import com.zjw.zy.componse.demo.bean.GameBean
import com.zjw.zy.componse.demo.ext.go
import com.zjw.zy.componse.demo.ui.theme.MyApplicationTheme
import com.zjw.zy.componse.demo.view.CommonEditView
import com.zjw.zy.componse.demo.viewmodel.GameListViewModel

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2023/2/16 15:36
 * @description：
 */
class GameListFragment : BaseComposeFragment() {

    override fun onContentView(rootView: ComposeView) {
        rootView.setContent {
            MyApplicationTheme {
                GameListView()

                AndroidView({
                            ImageView(it)
                }, modifier = Modifier,)
            }
        }
    }

    override fun onSubscribeValue() {

    }


    @Composable
    @Preview
    fun GameListView() {


        val vm: GameListViewModel = viewModel()

        vm.requireGameList()

        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(10.dp)
        ) {
            CommonEditView(
                leftIcon = R.drawable.icon_search,
                modifier = Modifier
                    .fillMaxWidth()
                    .weight(1f), hint = "搜索内容",
                onValueChange = {
                    //这里改变状态，过滤条件
                    vm.requireGameList(it)
                }
            )

            LazyColumn(modifier = Modifier.weight(10f), contentPadding = PaddingValues(10.dp)) {

                val list = vm.list.value
                for (i in list.indices) {
                    item {
                        GameItem(index = i, list[i])
                    }
                }
            }
            Row(
                modifier = Modifier
                    .weight(1f)
                    .padding(10.dp)
            ) {
                Image(
                    painter = painterResource(id = R.drawable.weather_icon_y1),
                    contentDescription = null,
                    modifier = Modifier
                        .fillMaxHeight()
                        .weight(1f)
                        .clickable {
                            go(R.id.mineGameFragment)
                        },
                )

                Image(
                    painter = painterResource(id = R.drawable.weather_icon_cloud1),
                    contentDescription = null,
                    modifier = Modifier
                        .fillMaxHeight()
                        .weight(1f),
                )

                Image(
                    painter = painterResource(id = R.drawable.weather_icon_rain1),
                    contentDescription = null,
                    modifier = Modifier
                        .fillMaxHeight()
                        .weight(1f),
                )

                Image(
                    painter = painterResource(id = R.drawable.weather_icon_night_sun_1),
                    contentDescription = null,
                    modifier = Modifier
                        .fillMaxHeight()
                        .weight(1f),
                )
            }
        }
    }


    @OptIn(ExperimentalFoundationApi::class)
    @Composable
    fun GameItem(index: Int, gameBean: GameBean) {

        //缓存上次重组的值，如果不变化则不发生重组
        val introduce = rememberSaveable {
            mutableStateOf(gameBean.name)
        }

        val image by remember {
            mutableStateOf(gameBean.image)
        }

        val play = rememberSaveable {
            mutableStateOf(false)
        }

        val colorAnimator by animateColorAsState(
            targetValue = if (play.value) Color.Blue else Color.White,
            animationSpec = tween(durationMillis = 3000)
        )

        Row(modifier = Modifier
            .fillMaxWidth()
            .height(150.dp)
            .padding(10.dp)
            .clickable {
                ToastUtils.showShort("click $index")
                //触发重组
                play.value = !play.value
            }
            .background(
                colorAnimator
            )
            .combinedClickable(enabled = true,
                onClickLabel = null,
                onLongClickLabel = "长按",
                onDoubleClick = {
                    ToastUtils.showShort("double click $index")
                },
                onLongClick = {
                    ToastUtils.showShort("long click $index")
                },
                onClick = {
                    go(gameBean.navId)
                })

        ) {
            Image(
                painter = painterResource(id = image),
                contentDescription = null,
                modifier = Modifier.size(140.dp, 140.dp),
            )

            Spacer(modifier = Modifier.width(10.dp))

            Text(text = introduce.value, modifier = Modifier.fillMaxWidth())
        }
    }

    override fun onBackPress() {
        requireActivity().moveTaskToBack(true)
    }
}
